<template>
  <div class="category">
    <el-button type="success" plain @click="showAdd">添加分类</el-button>
    <!-- 表格 -->
    <el-table :data="categoryList" row-key="cat_id">
      <el-table-column label="分类名称" prop="cat_name"></el-table-column>
      <el-table-column label="是否有效" prop="cat_name">
        <template v-slot="{row}">
          {{ row.cat_deleted?'否':'是' }}
        </template>
      </el-table-column>
      <el-table-column label="层级" prop="cat_level"></el-table-column>
      <el-table-column label="操作" prop="">
        <template>
          <el-button type="primary" size="small" plain icon="el-icon-edit"></el-button>
          <el-button type="danger" size="small" plain icon="el-icon-delete"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <!-- 添加模态框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addVisible"
      width="40%"
      @close="handleCloseDialog"
      >
      <el-form ref="addForm" :model="addForm" :rules="rules" label-width="80px">
        <el-form-item label="父级分类" prop="cat_name">
          <el-input placeholder="请输入父级分类" v-model="addForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级名称">
          <el-cascader
            v-model="addForm.cat_pid"
            :options="options"
            :props="props"
            clearable
            >
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCategory">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      type: '',
      pagenum: 1,
      pagesize: 5,
      total: 0,
      categoryList: [],
      addVisible: false,
      addForm: {
        cat_pid: '',
        cat_name: '',
        cat_level: ''
      },
      options: [],
      props: {
        value: 'cat_id',
        label: 'cat_name',
        checkStrictly: true

      },
      rules: {
        cat_name: [
          { required: true, message: '请输入活动名称', trigger: ['blur', 'change'] },
          { min: 3, message: '长度在至少为3个字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created () {
    this.getCategoryList()
  },
  methods: {
    async getCategoryList () {
      const { meta, data } = await this.$axios.get('categories', {
        params: {
          type: 3,
          pagenum: this.pagenum,
          pagesize: this.pagesize
        }
      })
      if (meta.status === 200) {
        this.total = data.total
        this.categoryList = data.result
        // console.log(data)
      }
    },
    handleSizeChange (val) {
      this.pagesize = val
      this.pagenum = 1
      this.getCategoryList()
    },
    handleCurrentChange (val) {
      this.pagenum = val
      this.getCategoryList()
    },
    async showAdd () {
      this.addVisible = true
      const { meta, data } = await this.$axios.get('categories?type=2')
      if (meta.status === 200) {
        this.options = data
        // console.log(data)
      }
    },
    async addCategory () {
      await this.$refs.addForm.validate()
      const arr = this.addForm.cat_pid || []
      this.addForm.cat_pid = arr[arr.length - 1] || 0
      this.addForm.cat_level = arr.length
      // console.log(this.addForm)
      const { meta } = await this.$axios.post('categories', this.addForm)
      if (meta.status === 201) {
        // console.log(meta)
        // console.log(data)
        this.$message.success(meta.msg)
        this.addVisible = false
        this.getCategoryList()
      } else {
        this.$message.error(meta.msg)
      }
    },
    handleCloseDialog () {
      this.$refs.addForm.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
  .category{
    .el-table{
      margin-top: 10px;
    }
  }
</style>
